#!/bin/bash
function install_docker() {
    echo "安装Docker" >>$logs_file
    if [ "$system_ver" != "7" ];then
        whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "不支持的操作系统版本！\n系统要求CentOS7 x86_64/amd64/ARM64/AARCH64" 10 70
        return 1
    fi
    installd_docker_ver=$(rpm -q docker-ce 2>/dev/null|awk -F"-" '{print $3}')
    if [ -n "$installd_docker_ver" ];then
        whiptail --backtitle "Author: Elvis" --title "警告" --yes-button "继续" --no-button "退出" --defaultno --yesno "系统已经安装docker-ce $installd_docker_ver，继续安装，现有版本可能被卸载！\n是否继续？" 8 70
        if [ "$?" != "0" ];then
          sleep 0.10
          return 1
        fi
    fi

    dockerVer=
    while [[ ! "$dockerVer" =~ ^[1-9][0-9]*\.[0-9]+\.[0-9]+$ ]]
    do
        dockerVer=$(whiptail --backtitle "Author: Elvis" --title "版本选择" --radiolist --nocancel \
        "请选择要安装的Docker版本？" 12 60 5 \
        "19.03.8" "      推荐                " ON \
        "18.09.9" "" OFF \
        "18.06.3" "" OFF \
        "17.12.1" "" OFF \
        "Other"  "      其他" OFF 3>&1 1>&2 2>&3)
        if [ "$dockerVer" == "Other" ];then
           dockerVer=$(whiptail --backtitle "Author: Elvis" --title "Docker版本" --inputbox --nocancel \
        "请输入要安装的Docker版本" 10 60 "17.09.1" 3>&1 1>&2 2>&3)
           if [[ ! "$dockerVer" =~ ^[1-9][0-9]*\.[0-9]+\.[0-9]+$ ]];then
                whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "Docker版本号只能由数字和.组成，且格式为格式为XXX.XXX.XXX，请重新输入！" 10 60
                sleep 0.3
           fi
        fi
    done
    dockerVer=`echo $dockerVer`
    dockerHome="$workHome/packages/docker"
    main_dockerVer=${dockerVer%.*}
    main_dockerVer_=${dockerVer%%.*}
    
    {
    echo 1
    sleep 1
    msg="检查并卸载已有docker-ce环境"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n20\n$msg\nXXX"
    echo 20
    yum remove -y docker-ce docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine >>$logs_file 2>&1
    if [ "$main_dockerVer_" == "17" ];then
        yum -y remove docker-ce-cli containerd.io container-selinux >>$logs_file 2>&1
    else
        yum -y remove docker-ce-selinux >>$logs_file 2>&1
    fi

    msg="添加Docker-ce YUM源"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n40\n$msg\nXXX"
    echo 40
    cat /etc/yum.repos.d/docker-ce.repo 2>/dev/null|grep -E "aliyun.com|tsinghua.edu.cn" >/dev/null 2>&1
    if [ "$?" != "0" ];then
        curl --fail --connect-timeout 30 -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo >>$logs_file 2>&1
        if [ "$?" != "0" ];then
          curl --fail --connect-timeout 30 -o /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo >>$logs_file 2>&1
          sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo >>$logs_file 2>&1
        fi
    fi

    offPack=`ls $dockerHome 2>/dev/null|grep "$dockerVer"|grep docker-ce|grep -v cli|grep ".rpm"`
    if [ -n "$offPack" ];then
        msg="检测到离线安装包，尝试离线安装"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n50\n$msg\nXXX"
        echo 50
        sleep 1
        all_offPack=`ls $dockerHome 2>/dev/null|grep "$dockerVer"|grep ".rpm"`
        
        if [ "$main_dockerVer" != "17.03" ];then
           install_command="yum localinstall -y --setopt=obsoletes=0 $dockerHome/$all_offPack $dockerHome/container*.rpm"
        else
           install_command="yum localinstall -y --setopt=obsoletes=0 $dockerHome/$all_offPack"
        fi
        echo -e "XXX\n55\n离线安装docker中.....\nXXX"
        echo 55
        $install_command >>$logs_file 2>&1
    else
        errrrrrrrr>/dev/null 2>&1
    fi
    
    
    if [ "$?" != "0" ];then
        msg="尝试使用在线安装"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n60\n$msg\nXXX"
        echo 60
        echo -e "XXX\n62\n在线安装docker中.....\nXXX"
        echo 62
        yum -y --setopt=obsoletes=0 install docker-ce-$dockerVer* docker-ce-cli-$dockerVer*>>$logs_file 2>&1
        if [ "$?" != "0" ];then
          msg="安装失败"
          echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
          echo -e "XXX\n65\n$msg\nXXX"
          echo 65
          return 1
        fi
    fi
    
    msg="启动docker"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n70\n$msg\nXXX"
    echo 70
    systemctl start docker >>$logs_file 2>&1
    
    msg="配置docker镜像加速"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n80\n$msg\nXXX"
    echo 80
    cat > /etc/docker/daemon.json <<EOF
{
    "registry-mirrors": ["https://r9tuk62b.mirror.aliyuncs.com", "https://docker.mirrors.ustc.edu.cn"],
    "exec-opts": ["native.cgroupdriver=systemd"],
    "log-driver": "json-file",
    "log-opts": {
    "max-file": "3",    
    "max-size": "100m"
    },
    "storage-driver": "overlay2",
    "storage-opts": [
      "overlay2.override_kernel_check=true"
    ]
}
EOF
    msg="重启docker并设置docker随机启动"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n90\n$msg\nXXX"
    echo 90
    systemctl restart docker >>$logs_file 2>&1&&systemctl enable docker >>$logs_file 2>&1

    msg="安装docker-compose，非必要安装"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n95\n$msg\nXXX"
    echo 95
    composeName="docker-compose-`uname -s`-`uname -m`"
    docker_compose=`ls $dockerHome 2>/dev/null|grep "$composeName"`
    if [ -n "$docker_compose" ];then
        /bin/cp -f $dockerHome/$docker_compose /usr/local/bin/docker-compose
        chmod +x /usr/local/bin/docker-compose
    else
        cd $dockerHome
        newest_ver=`git ls-remote --tags https://github.com/docker/compose.git 2>/dev/null|awk -F"/" '{print $3}'|egrep -v "docs|rc|test" |grep -v "v"|sort -rV|head -n1`
        newest_ver=${newest_ver:-1.25.5}
        curl --connect-timeout 30 -m 360 --fail -L -O https://get.daocloud.io/docker/compose/releases/download/$newest_ver/docker-compose-`uname -s`-`uname -m` >>$logs_file 2>&1
        if [ "$?" == "0" ];then
          /bin/cp -f docker-compose-`uname -s`-`uname -m` /usr/local/bin/docker-compose
          chmod +x /usr/local/bin/docker-compose
        else
          rm -rf docker-compose-`uname -s`-`uname -m`
        fi
    fi
    
    echo -e "XXX\n100\ndone！！！\nXXX"
    echo 100
    sleep 1 
    } | whiptail --backtitle "Author: Elvis" --title "安装Docker" --gauge " " 6 60 0
    
    #判断是否安装成功
    dockerPid=`pidof dockerd`
    docker_compose_ver=`docker-compose -v 2>/dev/null|awk '{print $3}'|sed 's/,//g'`
    
    if [ -n "$dockerPid" -a -n "$docker_compose_ver" ];then
        msg="Dcoker $dockerVer以及docker-compose $docker_compose_ver安装成功！"
        echo "$msg" >>$logs_file
        whiptail --backtitle "Author: Elvis" --title "消息" --msgbox "$msg" 8 70
    elif [ -n "$dockerPid" -a -z "$docker_compose_ver" ];then
        msg="Dcoker $dockerVer安装成功，但是docker-compose安装失败，不影响docker使用！"
        echo "$msg" >>$logs_file
        whiptail --backtitle "Author: Elvis" --title "消息" --msgbox "$msg" 8 70
    elif [ -z "$dockerPid" -a -n "$docker_compose_ver" ];then
        msg="Dcoker $dockerVer安装失败，docker-compose $docker_compose_ver安装成功！"
        echo "$msg" >>$logs_file
        whiptail --backtitle "Author: Elvis" --title "消息" --msgbox "$msg" 8 70
    else
        msg="Dcoker $dockerVer及docker-compose安装失败！"
        whiptail --backtitle "Author: Elvis" --title "消息" --msgbox "$msg" 8 70
    fi
    sleep 0.5
}